package com.bdqn.qnnews.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.qnnews.articel.api.dto.ApArticleDto;
import com.bdqn.qnnews.article.convert.ApArticleConvert;
import com.bdqn.qnnews.article.entity.ApArticle;
import com.bdqn.qnnews.article.entity.ApArticleConfig;
import com.bdqn.qnnews.article.entity.ApArticleContent;
import com.bdqn.qnnews.article.mapper.ApArticleConfigMapper;
import com.bdqn.qnnews.article.mapper.ApArticleContentMapper;
import com.bdqn.qnnews.article.mapper.ApArticleMapper;
import com.bdqn.qnnews.article.service.ApArticleService;
import com.bdqn.qnnews.article.service.ArticleFreemarkerService;
import com.bdqn.qnnews.article.vo.ArticleVo;
import com.bdqn.qnnews.common.constants.MQConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author xlz
 * @since 2023-12-22
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    private final ApArticleConfigMapper apArticleConfigMapper;
    private final ApArticleContentMapper apArticleContentMapper;
    private final ArticleFreemarkerService freemarkerService;
    private final RocketMQTemplate rocketMQTemplate;

    @Override
    public List<ApArticle> loadApArticle(ArticleVo avo, int loadType) {
        return baseMapper.loadApArticle(avo,loadType);
    }


    @Override
    public Long saveAndUpdateArticle(ApArticleDto dto) {

        // 将参数dto转换为文章实体对象
        final ApArticle apArticle = ApArticleConvert.INSTANCE.dtoToEntity(dto);
        // 判断当前是新增还是修改
        if (dto.getId() == null){
            // 没有id就是新增
            save(apArticle);

            // 保存文章配置表数据
            // 新增ap_article_config表
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setForward(true);
            apArticleConfig.setComment(true);
            // 插入数据到数据库表
            apArticleConfigMapper.insert(apArticleConfig);

            //新增ap_article_content表
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            // 插入数据到数据库表
            apArticleContentMapper.insert(apArticleContent);
        }else {
            // 修改
            updateById(apArticle);

            // 修改内容表
            //修改ap_article_content表
            QueryWrapper<ApArticleContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("article_id",apArticle.getId());
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(queryWrapper);
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }

        // 生成静态页，保存到minio中
        freemarkerService.buildArticleToMinIo(apArticle,dto.getContent());

        // 发起更新es库的请求(添加和删除索引)
        rocketMQTemplate.convertAndSend(MQConstants.QN_NEWS_UP_ES_TOPIC,apArticle);
        log.info("--文章发布成功，并发送索引库更新通知   文章id: {}",apArticle.getId());

        // 返回文章的id
        return apArticle.getId();
    }
}